package yoyo.secret.app.widget;

import yoyo.secret.app.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomTextView extends RelativeLayout {

	private static final int ID_BTN1 = 1;
	private static final int ID_BTN3 = 3;

	private int hintString;
	private int background;
	private int textViewColor;
	private int editText_foucs_padding;
	private int lable;

	private TextView textView;
	private EditText editText;
	private Object mobject;

	private void initLayout() {

		textView.setId(ID_BTN1);
		editText.setId(ID_BTN3);

		editText.setBackgroundResource(background);
		editText.setEms(10);
		editText.setHint(hintString);
		editText.setPadding(editText_foucs_padding, 0, 0, 0);

		RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		lp1.setMargins(0, 10, 0, 0);
		addView(editText, lp1);

		textView.setGravity(Gravity.CENTER);
		textView.setTextColor(textViewColor);
		textView.setText(lable);
		textView.setTextSize(25.0f);
		textView.setPadding(5, 0, 0, 0);

		RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp2.addRule(RelativeLayout.ALIGN_BASELINE, ID_BTN1);
		lp2.addRule(RelativeLayout.ALIGN_BOTTOM, ID_BTN1);
		lp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		lp2.setMargins(2, 0, 0, 0);
		addView(textView, lp2);

	}

	public CustomTextView(Context context) {
		this(context, null);
	}

	public CustomTextView(Context context, AttributeSet attrs) {
		super(context, attrs);

		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);

		hintString = a.getResourceId(R.styleable.CustomTextView_textHint, 0);
		background = a.getResourceId(R.styleable.CustomTextView_text_background, 0);
		textViewColor = a.getColor(R.styleable.CustomTextView_text_Color, 0);
		editText_foucs_padding = a.getDimensionPixelSize(R.styleable.CustomTextView_fouces_padding, 65);
		lable = a.getResourceId(R.styleable.CustomTextView_label, 0);

		textView = new TextView(context);
		editText = new EditText(context);
		initLayout();

		a.recycle();
	}

	public void setLableFirst(int text) {
		this.textView.setText(text);
	}

	public void setLableFirst(String text) {
		this.textView.setText(text);
	}

	public void setEditTextVaule(CharSequence text) {
		editText.setText(text);
	}

	public void bindData(Object object) {
		this.mobject = object;
	}

	public Object getBindData() {
		return mobject;
	}

	public CharSequence getEditTextVaule() {
		return this.editText.getText();
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		if (isInEditMode()) {
			return;
		}
		super.onLayout(changed, l, t, r, b);
	}

	@Override
	protected void onFinishInflate() {
		if (isInEditMode()) {
			return;
		}
		super.onFinishInflate();
	}
}
